import * as actions from '../actions/index';

// 页面初始化数据
export const initialState = {
  webhooks: [],
  isLoadingTable: true,
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.RECEIVE_WEBHOOKS:
      return { ...state, webhooks: action.data, isLoadingTable: false };
    case actions.TEST_RESULT:
      return { ...state, webhooks: action.data };
    case actions.REMOVE_WEBHOOK:
      return { ...state, webhooks: state.webhooks.filter((e) => e.id !== action.data) };
    case actions.CREATE_WEBHOOK:
      state.webhooks.unshift(action.data);
      return { ...state, webhooks: [].concat(state.webhooks) };
    default:
      return state;
  }
}
